package com.example.finalssm.Controller;

import com.example.finalssm.mapper.QuestionMapper;
import com.example.finalssm.pojo.Question;
import com.example.finalssm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/blogger")
public class AskQuestionController {
    //根据前端传进来的数据进行数据库操作，保存他的问题
    @ResponseBody
    @RequestMapping("/askQuestion")
    public String addQuestion(@RequestParam("question") String question, HttpSession session){
        String userId = (String)session.getAttribute("userId");
        Map<String,String> question1=new HashMap<>();
        question1.put("question",question);
        question1.put("userId",userId);
        int i = MybatisUtils.getSqlSession().getMapper(QuestionMapper.class).askQuestion(question1);
        if(i==1)return "提问失败";
        else return "保存问题成功";
    }
    @ResponseBody
    @RequestMapping("/upDateQuestion")
    public String upDateQuestion(@RequestParam("question") String question,@RequestParam("questionId") String id){
        int i = MybatisUtils.getSqlSession().getMapper(QuestionMapper.class).updateQuestion(question,id);
        if(i==1)return "提问失败";
        else return "保存问题成功";
    }
}
